<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-suffix="：" label-width="110px">
      <el-form-item label="学校名称" prop="schoolName">
        <el-input v-model="queryParams.schoolName" placeholder="请输入学校名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="社会信用代码" prop="creditCode">
        <el-input v-model="queryParams.creditCode" placeholder="请输入社会信用代码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="法定代表人" prop="legalPerson">
        <el-input v-model="queryParams.legalPerson" placeholder="请输入法定代表人" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="所属地区" prop="areaCode">
        <el-input v-model="queryParams.areaCode" placeholder="请输入所属地区" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['regulatory:school:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['regulatory:school:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schoolList" border>
      <el-table-column label="" align="center" prop="id" width="60"/>
      <el-table-column label="学校名称" align="center" prop="schoolName" min-width="200"/>
      <el-table-column label="所属管理单位" align="center" prop="bureauName" min-width="200"/>
      <el-table-column label="社会信用代码" align="center" prop="creditCode" width="160"/>
      <el-table-column label="办学许可证号" align="center" prop="schoolPermitCode" width="160"/>
      <el-table-column label="法定代表人" align="center" prop="legalPerson" width="120"/>
      <el-table-column label="所属地区" align="center" prop="areaCode"/>
      <el-table-column label="详细地址" align="center" prop="address"/>
      <el-table-column label="联系人" align="center" prop="contactName"/>
      <el-table-column label="联系电话" align="center" prop="contactTel"/>
      <el-table-column label="学校状态" align="center" prop="state" fixed="right" width="90">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.basics_training_state" :value="row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="260">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-user" @click="handleShowAdmin(scope.row)">管理员</el-button>
          <el-button size="mini" type="text" icon="el-icon-user" @click="handleClass(scope.row)">学校班级</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['regulatory:school:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['regulatory:school:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改学校基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="660px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-suffix="：">
        <el-form-item label="所属管理单位" prop="bureauId">
          <el-select v-model="form.bureauId" placeholder="请输入所属管理单位" style="width: 500px;" filterable clearable>
            <el-option v-for="bureau in bureauList" :key="bureau.id" :label="bureau.name" :value="bureau.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="form.schoolName" placeholder="请输入学校名称"/>
        </el-form-item>
        <el-form-item label="社会信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入社会信用代码"/>
        </el-form-item>
        <el-form-item label="办学许可证号" prop="schoolPermitCode">
          <el-input v-model="form.schoolPermitCode" placeholder="请输入办学许可证号"/>
        </el-form-item>
        <el-form-item label="法定代表人" prop="legalPerson">
          <el-input v-model="form.legalPerson" placeholder="请输入法定代表人"/>
        </el-form-item>
        <el-form-item label="所属地区" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入所属地区"/>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="联系人" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入联系人"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactTel">
          <el-input v-model="form.contactTel" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="学校简介" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入学校简介"/>
        </el-form-item>
        <el-form-item label="学校logo" prop="logo">
          <el-input v-model="form.logo" placeholder="请输入学校logo"/>
        </el-form-item>
        <el-form-item label="学校状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择学校状态" clearable>
            <el-option v-for="dict in dict.type.basics_training_state" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <RegulatoryAdmin ref="regulatoryAdminRef" user-type="02"/>
    <SchoolClass ref="schoolClassRef"/>
  </div>
</template>

<script>
import {listSchool, getSchool, delSchool, addSchool, updateSchool} from "@/api/regulatory/school";
import {allBureau} from "@/api/regulatory/bureau";
import RegulatoryAdmin from '@/views/regulatory/components/regulatory_admin'
import SchoolClass from '@/views/regulatory/components/school_class'

export default {
  name: "School",
  components: {RegulatoryAdmin, SchoolClass},
  dicts: ['basics_training_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学校基础信息表格数据
      schoolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      bureauList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolCode: null,
        schoolName: null,
        creditCode: null,
        schoolPermitCode: null,
        legalPerson: null,
        areaCode: null,
        servicesMobile: null,
        homepage: null,
        logo: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bureauId: [{required: true, message: '请输入所属管理单位', trigger: 'blur'}],
        schoolName: [{required: true, message: '请输入学校名称', trigger: 'blur'}],
      }
    };
  },
  created() {
    this.getList()
    this.getBureauList()
  },
  methods: {
    /** 查询学校基础信息列表 */
    getList() {
      this.loading = true;
      listSchool(this.queryParams).then(response => {
        this.schoolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bureauId: null,
        schoolCode: null,
        schoolName: null,
        creditCode: null,
        schoolPermitCode: null,
        legalPerson: null,
        areaCode: null,
        address: null,
        contactName: null,
        contactTel: null,
        remark: null,
        servicesMobile: null,
        homepage: null,
        logo: null,
        state: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学校基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchool(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学校基础信息";
      });
    },
    getBureauList() {
      allBureau().then(response => {
        this.bureauList = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSchool(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchool(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学校基础信息编号为"' + ids + '"的数据项？').then(function () {
        return delSchool(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('regulatory/school/export', {
        ...this.queryParams
      }, `school_${new Date().getTime()}.xlsx`)
    },
    handleShowAdmin(row) {
      this.$refs.regulatoryAdminRef.openAdmin(row.id)
    },
    handleClass(row) {
      this.$refs.schoolClassRef.openClass(row.id)
    },
  }
};
</script>
